﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Request;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class UpdateUserQuestionServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateUserQuestionRequest, ResponseRecord>
    {
        public UpdateUserQuestionServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateUserQuestionRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.Record == null)
            {
                throw new ArgumentException("Record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            var userQuestion = new UserQuestion
            {
                Title = record.Title,
                QuestionId = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString(),
                Status = 0,
                Created = DateTime.Now,
                Username = record.Username
            };

            try
            {
                bool savedUser = Providers.Users.SaveUserQuestion(userQuestion);
                response.IsSuccess = savedUser;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.IsSuccess = false;
            }
            return response.IsSuccess;
        }
    }
}
